package com.example.zhaojianxu.signin.securities.sell

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.common.constance.ComConstance
import com.example.zhaojianxu.signin.common.http.HttpUtil
import com.example.zhaojianxu.signin.securities.http.BuyEntity
import com.example.zhaojianxu.signin.securities.http.SellEntity
import kotlinx.android.synthetic.main.activity_sell.*
import kotlinx.android.synthetic.main.common_title_bar.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SellActivity : BaseActivity() {

    var mAdapter: SellAdapter? = null
    val mList: MutableList<SellEntity> = mutableListOf()

    override fun initView() {
        common_title.text = "股票抛售"
        common_setting.visibility = View.GONE
        common_back.setOnClickListener {
            finish()
        }

        mAdapter = SellAdapter(mList)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        activity_sell_recycler.layoutManager = layoutManager
        activity_sell_recycler.adapter = mAdapter

        activity_sell_swipe.setOnRefreshListener { initData() }
    }

    override fun initData() {

        HttpUtil.instance.getSecuritiesApi()?.postQuerySell(ComConstance.UID)?.enqueue(object: Callback<MutableList<SellEntity>>{
            override fun onFailure(call: Call<MutableList<SellEntity>>, t: Throwable) {

            }

            override fun onResponse(call: Call<MutableList<SellEntity>>, response: Response<MutableList<SellEntity>>) {
                if (response.isSuccessful && response.body() != null){
                    mAdapter?.mList = response.body()!!
                    mAdapter?.notifyDataSetChanged()
                    activity_sell_swipe.isRefreshing = false
                }
            }

        })


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sell)
    }
}
